<?php
/*
 * 自定义工具包
 */
class UtilClass
{
	public function init()
	{
		
	}
	
	public function js()
	{
		$js_num = func_num_args();
		$js_files = func_get_args();
		if ($js_num == 1 && is_array($js_files[0])) $js_files = $js_files[0];
		foreach ($js_files as $js_file)
		{
			echo '<script type="text/javascript" src="'. BASE_URL . $js_file .'?'. JS_VERSION .'"></script>' . PHP_EOL;
		}
	}
	
	public function css()
	{
		$css_num = func_num_args();
		$css_files = func_get_args();
		if ($css_num == 1 && is_array($css_files[0])) $css_files = $css_files[0];
		foreach ($css_files as $css_file)
		{
			echo '<link rel="stylesheet" type="text/css" href="'. BASE_URL . $css_file .'?'. CSS_VERSION .'" />' . PHP_EOL;
		}
	}
	
	public function sub_string($str, $start=0, $length=null, $charset="utf-8") 
	{
		if(function_exists("mb_substr")) {
			$length = empty($length) ? mb_strlen($str, $charset) : $length;
			if(mb_strlen($str,$charset) <= $length) {
				return $str;
			}
			$slice = mb_substr($str, $start, $length, $charset);
		} else {
			$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
			$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
			$re['gbk']	= "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
			$re['big5']	= "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
			preg_match_all($re[$charset], $str, $match);
			$length = is_null($length) ? count($match[0]) : $length;
			if(count($match[0]) <= $length) {
				return $str;
			}
			$slice = implode("", array_slice($match[0], $start, $length));
		}
		return $slice;
	}
	
	public function show_404()
	{
		header("HTTP/1.1 404 Not Found");
		exit('404 Not Found');
		Yii::app()->controller->renderFile(Yii::app()->viewPath . '/errors/404.php');
		die;
	}
	
	public function return_json($msg = '', $status = true, $url = '')
	{
		echo json_encode(array(
			'msg' => $msg,
			'status' => $status,
			'url' => $url
		));
		die;
	}
}